<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3
 * Time: 12:24
 */

namespace Admin\Model;
use Think\Model;

class ApplicationModel extends Model
{
    protected $tableName = "application";
    protected $tablePrefix = "dangjian_";
    protected $showRow = 10;

    //查询所有申请记录
    public function queryAllApp($data)
    {
        //搜索条件
        $name = $data['name'];
        $time = $data['time'];
        $status = $data['status'];
        $order = $data['order'];//排序字段
        $order1 = $data['order1'];//排序方式

        $map = array();
        if(!empty($name)){
            $map['u.name'] = array("LIKE", '%' . $name . '%');
        }
        if(!empty($time)){
            $start = substr($time,0,10);
            $end = substr($time,13,10);
            $start = date("Y-m-d H:i:s",strtotime($start));
            $end = date("Y-m-d H:i:s",strtotime($end));
            $map['a.applica_time'] = array(array("gt",$start),array("lt",$end),"and");
        }
        if(!empty($status)){
            if($status == 2){
                $status = 0;
            }
            $map['a.applica_status'] = $status;
        }

        if(empty($order)){
            $order = "a.id";
        }

        if(empty($order1)){
            $order1 = "asc";
        }

        $count = $this->alias("a")
            ->join("dangjian_user u on u.id=a.user_id")
            ->where($map)
            ->count();
        $page = new \Think\Page($count,$this->showRow);
        $show = $page->show();
        $list = $this->alias("a")
            ->join("dangjian_user u on u.id=a.user_id")
            ->join("left join dangjian_user uu on uu.id = a.approve_id")
            ->field("a.id,u.name,u.fee_time,a.department,a.applica_time,a.applica_status,uu.name approve_name,a.approve_time")
            ->where($map)
            ->order("$order $order1")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $res['show'] = $show;
        $res['list'] = $list;
        return $res;
    }

    //把组织关系转出申请信息,保存到数据库
    public function saveApplication($data)
    {
        $res = $this->add($data);
        if($res > 0){
            return 1;
        }
        return 0;
    }

    //根据id查询申请记录详情
    public function queryInfoById($id)
    {
        $res = $this->alias("a")
            ->join("left join dangjian_user u on u.id=a.user_id")
            ->join("left join dangjian_user uu on uu.id = a.approve_id")
            ->field("a.id,u.name,u.fee_time,a.department,a.out_department,a.applica_time,a.applica_status,uu.name approve_name,a.approve_time,u.card_num,u.tel,u.join_time")
            ->where("a.id=$id")
            ->find();
        return $res;
    }
}